package com.bruce.tool.orm.mybatis.generator.plugin;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.config.CommentGeneratorConfiguration;
import org.springframework.util.CollectionUtils;

import java.sql.Types;
import java.util.List;

/**
 * 功能 :
 * 1.增加生成example对象配置.
 * 2.增加字段类型自动转换配置.
 * 3.增加主键自动设置配置.
 *
 * @author : Bruce(刘正航) 10:44 上午 2020/1/10
 */
public class InitPlugin extends PluginAdapter {
    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public void initialized(IntrospectedTable introspectedTable) {
        // 抑制原生generator生成的注释
        CommentGeneratorConfiguration configuration = new CommentGeneratorConfiguration();
        configuration.setConfigurationType("org.mybatis.generator.internal.DefaultCommentGenerator");
        configuration.getProperties().setProperty("suppressAllComments", "true");
        this.context.setCommentGeneratorConfiguration(configuration);
        super.initialized(introspectedTable);
        // 设置生成example类的标签属性
        introspectedTable.getTableConfiguration().setSelectByExampleStatementEnabled(true);
        List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
        if (!CollectionUtils.isEmpty(primaryKeyColumns) && primaryKeyColumns.size() == 1) {
            // 设置主键标记为true
            for (IntrospectedColumn primary : primaryKeyColumns) {
                primary.setIdentity(true);
            }
        }

        List<IntrospectedColumn> baseColumns = introspectedTable.getBaseColumns();
        if (!CollectionUtils.isEmpty(baseColumns)) {
            // 设置字段类型: 特殊类型转换(text-->String)(tinyint-->Integer/boolean)(byte-->Integer/Boolean)
            for (IntrospectedColumn column : baseColumns) {
                //tinyint(2-->4)
                if (Types.TINYINT == column.getJdbcType()) {
                    column.setFullyQualifiedJavaType(new FullyQualifiedJavaType("java.lang.Integer"));
                }
                // bit|tinyint(1)
                if (Types.BIT == column.getJdbcType()) {
                    column.setFullyQualifiedJavaType(new FullyQualifiedJavaType("java.lang.Boolean"));
                }
            }
        }
        if (!CollectionUtils.isEmpty(primaryKeyColumns) && primaryKeyColumns.size() > 1) {
            baseColumns.addAll(primaryKeyColumns);
            primaryKeyColumns.clear();
        }
        List<IntrospectedColumn> blobColumns = introspectedTable.getBLOBColumns();
        if (!CollectionUtils.isEmpty(blobColumns)) {
            for (IntrospectedColumn column : blobColumns) {
                if (Types.LONGVARCHAR == column.getJdbcType()) {
                    column.setFullyQualifiedJavaType(new FullyQualifiedJavaType("java.lang.String"));
                }
            }
            baseColumns.addAll(blobColumns);
            blobColumns.clear();
        }
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        return super.contextGenerateAdditionalJavaFiles(introspectedTable);
    }

}
